package com.czxy.service;

import com.czxy.dao.UserMapper;
import com.czxy.domain.User;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional
public class UserServiceImpl implements UserService{

    @Resource
    private UserMapper userMapper;


    @Override
    public List<User> findAll() {
        return userMapper.selectAll();
    }

    @Override
    public List<User> findByInfo(User user) {


        //拼条件
        Example example = new Example(User.class);
        Example.Criteria ct = example.createCriteria();



        //包含用户名
        if (user.getLoginName()!=null && !"".equals(user.getLoginName())){
            ct.andLike("loginName","%"+user.getLoginName()+"%");

        }
        //学历
        if (user.getEducation() != null && !"".equals(user.getEducation())){
            ct.andEqualTo("education",user.getEducation());

        }
        //性别
        if (user.getSex() != null &&  !"".equals(user.getSex())){
            ct.andEqualTo("sex",user.getSex());
        }

        List<User> list = userMapper.selectByExample(example);


        return list;
    }

    @Override
    public boolean add(User user) {
        //校验登录名
        Example example = new Example(User.class);
        Example.Criteria ct = example.createCriteria();
        ct.andEqualTo("loginName",user.getLoginName());

        List<User> list = userMapper.selectByExample(example);
        if (list.size() > 0){

            return false;
        }


        //添加
        userMapper.insert(user);


        return true;
    }
}



























